package com.viphuli.app.tool.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.loopj.android.http.RequestParams;
import com.viphuli.app.tool.R;
import com.viphuli.app.tool.common.AccessTokenKeeper;
import com.viphuli.app.tool.common.MyPageHelper;
import com.viphuli.app.tool.handler.MyBaseHttpResponseHandler.HandlerType;
import com.viphuli.app.tool.http.ApiRequest;

/**
 * 排班视图（周&月）
 * 
 * @author li.li
 *
 */
public class ArrangeViewFragment extends BaseProgressFragment implements OnCheckedChangeListener {

	public static final int ACTION_ARRANGE = 1;//排班
	public static final int ACTION_EXCHANGE = 2;//换班&替班
	public static final String ACTION_TYPE = "action_type";

	private int actionType;

	//默认选择的人
	private String defSelectUid;
	//默认选择的班次
	private long defSelectTime;

	private RadioGroup tabs;

	private Fragment frag;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_arrange_view;
	}

	@Override
	protected void initParams() {
		Bundle args = getArguments();

		//操作类型
		actionType = args.getInt("action_type", 0);

		//默认选择某个班次
		defSelectUid = args.getString("def_select_uid", "");
		defSelectTime = args.getLong("def_select_time", 0);
	}

	@Override
	protected void initView() {

		tabs = caller.getArrangeTab();
		tabs.setVisibility(View.VISIBLE);
		caller.getCustomTitle().setVisibility(View.GONE);
		tabs.setOnCheckedChangeListener(this);

	}

	@Override
	protected void initData() {
		//默认选择周视图
		RadioButton radioBtn = (RadioButton) tabs.getChildAt(0);
		radioBtn.setChecked(true);

	}

	@Override
	public void onCheckedChanged(RadioGroup button, int checkedId) {

		Bundle args = new Bundle();
		args.putInt(ACTION_TYPE, actionType);

		switch (checkedId) {
		case R.id.id_arrange_tab_week:

			args.putString("def_select_uid", defSelectUid);
			args.putLong("def_select_time", defSelectTime);

			frag = new ArrangeViewWeekFragment();
			frag.setArguments(args);

			selectTab(frag);

			break;

		case R.id.id_arrange_tab_month:

			frag = new ArrangeViewMonthFragment();
			frag.setArguments(args);

			selectTab(frag);

			break;

		default:
			break;
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		if (ArrangeViewFragment.ACTION_ARRANGE == actionType) {//排班

			getActivity().getMenuInflater().inflate(R.menu.menu_arrange_view, menu);

		} else if (ArrangeViewFragment.ACTION_EXCHANGE == actionType) {//替班&换班

			getActivity().getMenuInflater().inflate(R.menu.menu_arrange_view_share, menu);

		}

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		if (R.id.id_menu_arrange_view_setting == item.getItemId()) {

			ArrangeTypeListFragment.go(getActivity());

			return true;

		} else if (R.id.id_menu_arrange_view_setting_share == item.getItemId()) {

			if (frag instanceof ArrangeViewWeekFragment) {

				ArrangeViewWeekFragment weekFrag = (ArrangeViewWeekFragment) frag;

				//分享排班信息
				AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(getActivity());

				if (tokenKeeper.isLogin()) {

					RequestParams params = new RequestParams();
					params.put("uid", tokenKeeper.getOpenId());
					params.put("circle_id", tokenKeeper.getUser().circleId());
					params.put("share_type", 2);
					params.put("which", weekFrag.getCurWhich());
					params.put("time", weekFrag.getTime());

					ApiRequest.circleWecahtShare.request(this, params, HandlerType.waitting, false);

				}

			}

			return true;

		}

		return false;
	}

	private void selectTab(Fragment frag) {

		if (frag == null || frag.isAdded())
			return;

		FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
		transaction.replace(R.id.id_content, frag);
		transaction.commitAllowingStateLoss();
	}

	public int getActionType() {
		return actionType;
	}

	public static void go(Activity act, Bundle args) {

		MyPageHelper.arrangeView.showMyPage(act, args);
	}

}
